home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
src
/
demos
/
GL
/
logo
/
logo.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
17KB
|
881 lines
/*
* Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
* the contents of this file may not be disclosed to third parties, copied or
* duplicated in any form, in whole or in part, without the prior written
* permission of Silicon Graphics, Inc.
*
* RESTRICTED RIGHTS LEGEND:
* Use, duplication or disclosure by the Government is subject to restrictions
* as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
* and Computer Software clause at DFARS 252.227-7013, and/or in similar or
* successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
* rights reserved under the Copyright Laws of the United States.
*/
/* logo.c
* Thant Tessman - July, '87
*
* (Three days and much ugly hacking.)
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <gl/gl.h>
#include <gl/device.h>
#include "light.h"
#define X 0
#define Y 1
#define Z 2
#define S_CYL 6.0
#define D_CYL 8.0
#define ELBOW_RAD 1.0
#define JOINT 900
float radius, s_cyl, d_cyl, elbow_rad;
int cres, bres;
Angle joint;
float mycirc[100][3];
int num;
short dev, val;
long gid;
float ident_matrix[4][4] = {
{1.0, 0.0, 0.0, 0.0},
{0.0, 1.0, 0.0, 0.0},
{0.0, 0.0, 1.0, 0.0},
{0.0, 0.0, 0.0, 1.0},
};
float view[4][4] = {
{1.0, 0.0, 0.0, 0.0},
{0.0, 1.0, 0.0, 0.0},
{0.0, 0.0, 1.0, 0.0},
{0.0, 0.0, 0.0, 1.0},
};
float tmp[4][4];
short mx, my, omx, omy, nmx, nmy;
int bend_left(), bend_right(), bend_forward(), move_single(), move_double();
typedef struct polygon_struct {
int n;
float *polygon; /* malloced 3 at a time */
float *norm; /* ditto */
Colorindex *c;
struct polygon_struct *next;
} Poly;
typedef struct thing_struct {
Poly *polygons;
int (*move)();
struct thing_struct *sub; /* sub things */
struct thing_struct *next; /* next (not affected by matrix) */
} Thing;
Poly *new_polygon() {
Poly *new;
new = (Poly *)malloc(sizeof(Poly));
new->n = 0;
new->polygon = NULL;
new->norm = NULL;
new->next = NULL;
return(new);
}
Thing *new_thing() {
int i, j;
Thing *new;
new = (Thing *)malloc(sizeof(Thing));
new->sub = NULL;
new->next = NULL;
return(new);
}
Thing *logo;
Poly *elbow, *double_cylinder, *single_cylinder;
int function=0;
#define REORIENT 1
main (argc, argv)
int argc;
char *argv[];
{
int needredraw = 1;
int i, j;
initialize(argv[0]);
while(TRUE) {
while(qtest() || (function == 0 && joint == JOINT && needredraw == 0)) {
dev=qread(&val);
switch(dev) {
case ESCKEY :
if (!val) {
gexit();
exit(0);
}
break;
case LEFTMOUSE :
/*qread(&mx); qread(&my);*/
if (val) {
reset_values();
for (i=0; i<4; i++) for (j=0; j<4; j++)
view[i][j] = (float)(i==j);
}
break;
case MIDDLEMOUSE:
/*qread(&omx); qread(&omy);*/
if (val) {
omx = getvaluator(MOUSEX);
omy = getvaluator(MOUSEY);
function = REORIENT;
}
else function = 0;
break;
case REDRAW:
reshapeviewport();
needredraw=1;
break;
}
}
nmx = getvaluator(MOUSEX);
nmy = getvaluator(MOUSEY);
switch(function) {
case REORIENT:
reorient();
break;
}
omx=nmx; omy=nmy;
draw_everything();
needredraw=0;
}
}
initialize(title)
char *title;
{
char tstr[1024];
char *t;
t = strrchr(title, '/');
t = (t == NULL ? title : t+1);
if (getgdesc(GD_BITS_NORM_DBL_RED) == 0)
{
sprintf(tstr,
"inform 'Your system must support double-buffered RGB mode to run %s'", t);
system(tstr);
exit(1);
}
if (getgdesc(GD_BITS_NORM_ZBUFFER) == 0)
{
sprintf(tstr,
"inform 'Your system must have a z-buffer to run %s'", t);
system(tstr);
exit(1);
}
keepaspect(5, 4);
gid=winopen(t);
doublebuffer();
RGBmode();
gconfig();
subpixel(TRUE);
RGBcolor(0, 0, 0);
clear(); swapbuffers(); clear();
qdevice(INPUTCHANGE);
qdevice(REDRAW);
qdevice(ESCKEY);
qdevice(LEFTMOUSE);
qdevice(MIDDLEMOUSE);
/*tie(LEFTMOUSE, MOUSEX, MOUSEY);*/
/*tie(MIDDLEMOUSE, MOUSEX, MOUSEY);*/
/* makerange(RAMPB, RAMPE, 50, 255, 0, 50, 50, 255); */
reset_values();
zbuffer(TRUE);
zclear();
mmode(MVIEWING);
loadmatrix(ident_matrix);
init_lighting();
build_logo();
}
draw_everything() {
int i, j;
float t;
if (joint>JOINT) {
joint-=9;
t = (float)(1800-joint)/(float)(JOINT);
s_cyl = t * S_CYL;
d_cyl = t * D_CYL;
edit_parts();
} else {
joint = JOINT;
}
RGBcolor(0, 0, 0);
clear();
zclear();
perspective(350, 5.0/4.0, 34, 72);
mmode(MVIEWING);
loadmatrix(ident_matrix);
lmbind(LIGHT0, OVER_LIGHT);
lmbind(LIGHT1, UNDER_LIGHT);
lookat(-30.0, 30.0, 30.0, 0.0, 0.0, 0.0, 0);
multmatrix(view);
t = (2.0*radius + 2*d_cyl - s_cyl)/2.0;
translate(t, -t, t);
draw_thing(logo);
swapbuffers();
}
build_logo() {
int i;
Thing *tmp;
build_parts();
logo = new_thing();
for (i=0; i<3; i++) {
if (i==0) {
tmp = logo;
} else {
tmp->sub = new_thing();
tmp=tmp->sub;
}
tmp->polygons = double_cylinder;
tmp->move = move_double;
tmp->sub = new_thing(); tmp = tmp->sub;
tmp->polygons = elbow;
tmp->move = bend_forward;
tmp->sub = new_thing(); tmp = tmp->sub;
tmp->polygons = double_cylinder;
tmp->move = move_double;
tmp->sub = new_thing(); tmp = tmp->sub;
tmp->polygons = elbow;
tmp->move = bend_forward;
tmp->sub = new_thing(); tmp = tmp->sub;
tmp->polygons = single_cylinder;
tmp->move = move_single;
tmp->sub = new_thing(); tmp = tmp->sub;
tmp->polygons = elbow;
tmp->move = bend_right;
tmp->sub = new_thing(); tmp = tmp->sub;
tmp->polygons = double_cylinder;
tmp->move = move_double;
tmp->sub = new_thing(); tmp = tmp->sub;
tmp->polygons = elbow;
tmp->move = bend_forward;
tmp->sub = new_thing(); tmp = tmp->sub;
tmp->polygons = double_cylinder;
tmp->move = move_double;
tmp->sub = new_thing(); tmp = tmp->sub;
tmp->polygons = elbow;
tmp->move = bend_forward;
tmp->sub = new_thing(); tmp = tmp->sub;
tmp->polygons = single_cylinder;
tmp->move = move_single;
tmp->sub = new_thing(); tmp = tmp->sub;
tmp->polygons = elbow;
tmp->move = bend_left;
}
}
bend_right() {
rotate(joint, 'z');
translate(0.0, elbow_rad, 0.0);
rotate(joint, 'x');
translate(0.0, -elbow_rad, 0.0);
}
int bend_left() {
rotate(-joint, 'z');
translate(0.0, elbow_rad, 0.0);
rotate(joint, 'x');
translate(0.0, -elbow_rad, 0.0);
}
bend_forward() {
translate(0.0, elbow_rad, 0.0);
rotate(joint, 'x');
translate(0.0, -elbow_rad, 0.0);
}
move_double() {
translate(0.0, 0.0, -d_cyl);
}
move_single() {
translate(0.0, 0.0, -s_cyl);
}
reorient() {
pushmatrix();
loadmatrix(ident_matrix);
rotate((Angle) (nmx-omx), 'y');
rotate((Angle) (omy-nmy), 'x');
multmatrix(view);
getmatrix(view);
popmatrix();
}
print_matrix(m)
float m[4][4];
{
int i, j;
for (i=0; i<4; i++) {
for (j=0; j<4; j++) {
printf("%8.4f ", m[i][j]);
}
printf("\n");
}
printf("\n");
}
reset_values() {
radius = 1.0;
s_cyl = 0.0;
d_cyl = 0.0;
elbow_rad = ELBOW_RAD;
cres = 8;
bres = 8;
joint = 1800;
}
draw_thing(thing)
Thing *thing;
{
while(thing) {
(*thing->move)();
draw_polygons(thing->polygons);
draw_thing(thing->sub);
thing = thing->next;
}
}
draw_polygons(polygon)
Poly *polygon;
{
int i;
lmbind(MATERIAL, MAT_LOGO);
lighting(TRUE);
while(polygon=polygon->next) {
bgntmesh();
n3f(&polygon->norm[0*3]);
v3f(&polygon->polygon[0*3]);
n3f(&polygon->norm[1*3]);
v3f(&polygon->polygon[1*3]);
n3f(&polygon->norm[3*3]);
v3f(&polygon->polygon[3*3]);
n3f(&polygon->norm[2*3]);
v3f(&polygon->polygon[2*3]);
endtmesh();
/*bgntmesh();
for (i=0; i<polygon->n; i++) {
n3f(&polygon->norm[i*3]);
v3f(&polygon->polygon[i*3]);
}
endtmesh();*/
/*bgnclosedline();
for (i=0; i<polygon->n; i++) {
n3f(&polygon->norm[i*3]);
v3f(&polygon->polygon[i*3]);
}
endclosedline();*/
/* poly(polygon->n, polygon->polygon);*/
/* splf(polygon->n, polygon->polygon, polygon->c);*/
/* I'm only going to draw the first normal as a test case. The rest are
really there (norm[3 * vertex_num])
*/
/* RGBcolor(BLUE);
{
float vect[3];
bgnline();
vect[0] = polygon->polygon[X];
vect[1] = polygon->polygon[Y];
vect[2] = polygon->polygon[Z];
v3f(vect);
vect[0] += polygon->norm[X];
vect[1] += polygon->norm[Y];
vect[2] += polygon->norm[Z];
v3f(vect);
endline();
}
*/
}
}
build_parts() {
float ct1[100][3], ct2[100][3];
float norm1[100][3], norm2[100][3];
Angle a;
float glsin, glcos;
int i;
Poly *polygon;
num = 0;
elbow = new_polygon();
polygon = elbow;
for (a=0; a<=3600; a += 3600/cres) {
gl_sincos(a, &mycirc[num][Y], &mycirc[num][X]);
mycirc[num++][Z] = 0.0;
}
for (i=0; i<num; i++) {
ct1[i][X] = mycirc[i][X];
ct1[i][Y] = mycirc[i][Y];
ct1[i][Z] = mycirc[i][Z];
norm1[i][X] = mycirc[i][X];
norm1[i][Y] = mycirc[i][Y];
norm1[i][Z] = mycirc[i][Z];
}
for (a=joint/bres; a<=joint; a += joint/bres) {
gl_sincos(a, &glsin, &glcos);
for (i=0; i<num; i++) {
ct2[i][X] = mycirc[i][X];
ct2[i][Y] = (mycirc[i][Y] - elbow_rad) * glcos + elbow_rad;
ct2[i][Z] = -(mycirc[i][Y]-elbow_rad) * glsin;
norm2[i][X] = mycirc[i][X];
norm2[i][Y] = mycirc[i][Y] * glcos;
norm2[i][Z] = -mycirc[i][Y] * glsin;
}
for (i=0; i<num-1; i++) {
polygon->next = new_polygon();
polygon = polygon->next;
polygon->polygon = (float *)malloc(sizeof(float) * 3 * 4);
polygon->norm = (float *)malloc(sizeof(float) * 3 * 4);
polygon->c = (Colorindex *)malloc(sizeof(float) * 4);
polygon->n=4;
polygon->polygon[X] = ct1[i][X];
polygon->polygon[Y] = ct1[i][Y];
polygon->polygon[Z] = ct1[i][Z];
polygon->norm[X] = norm1[i][X];
polygon->norm[Y] = norm1[i][Y];
polygon->norm[Z] = norm1[i][Z];
polygon->polygon[3+X] = ct2[i][X];
polygon->polygon[3+Y] = ct2[i][Y];
polygon->polygon[3+Z] = ct2[i][Z];
polygon->norm[3+X] = norm2[i][X];
polygon->norm[3+Y] = norm2[i][Y];
polygon->norm[3+Z] = norm2[i][Z];
polygon->polygon[6+X] = ct2[i+1][X];
polygon->polygon[6+Y] = ct2[i+1][Y];
polygon->polygon[6+Z] = ct2[i+1][Z];
polygon->norm[6+X] = norm2[i+1][X];
polygon->norm[6+Y] = norm2[i+1][Y];
polygon->norm[6+Z] = norm2[i+1][Z];
polygon->polygon[9+X] = ct1[i+1][X];
polygon->polygon[9+Y] = ct1[i+1][Y];
polygon->polygon[9+Z] = ct1[i+1][Z];
polygon->norm[9+X] = norm2[i+1][X];
polygon->norm[9+Y] = norm2[i+1][Y];
polygon->norm[9+Z] = norm2[i+1][Z];
}
for (i=0; i<num; i++) {
ct1[i][X] = ct2[i][X];
ct1[i][Y] = ct2[i][Y];
ct1[i][Z] = ct2[i][Z];
norm1[i][X] = norm2[i][X];
norm1[i][Y] = norm2[i][Y];
norm1[i][Z] = norm2[i][Z];
}
}
double_cylinder = new_polygon();
polygon = double_cylinder;
gl_sincos(0, &glsin, &glcos);
for (a = 3600/cres; a<=3600; a += 3600/cres) {
polygon->next = new_polygon();
polygon = polygon->next;
polygon->polygon = (float *)malloc(sizeof(float) * 3 * 4);
polygon->norm = (float *)malloc(sizeof(float) * 3 * 4);
polygon->c = (Colorindex *)malloc(sizeof(float) * 4);
polygon->n=4;
polygon->polygon[X] = glcos;
polygon->polygon[Y] = glsin;
polygon->polygon[Z] = -0.1;
polygon->norm[X] = glcos;
polygon->norm[Y] = glsin;
polygon->norm[Z] = 0.0;
polygon->polygon[3+X] = glcos;
polygon->polygon[3+Y] = glsin;
polygon->polygon[3+Z] = d_cyl;
polygon->norm[3+X] = glcos;
polygon->norm[3+Y] = glsin;
polygon->norm[3+Z] = 0.0;
gl_sincos(a, &glsin, &glcos);
polygon->polygon[6+X] = glcos;
polygon->polygon[6+Y] = glsin;
polygon->polygon[6+Z] = d_cyl;
polygon->norm[6+X] = glcos;
polygon->norm[6+Y] = glsin;
polygon->norm[6+Z] = 0.0;
polygon->polygon[9+X] = glcos;
polygon->polygon[9+Y] = glsin;
polygon->polygon[9+Z] = -0.1;
polygon->norm[9+X] = glcos;
polygon->norm[9+Y] = glsin;
polygon->norm[9+Z] = 0.0;
}
single_cylinder = new_polygon();
polygon = single_cylinder;
gl_sincos(0, &glsin, &glcos);
for (a = 3600/cres; a<=3600; a += 3600/cres) {
polygon->next = new_polygon();
polygon = polygon->next;
polygon->polygon = (float *)malloc(sizeof(float) * 3 * 4);
polygon->norm = (float *)malloc(sizeof(float) * 3 * 4);
polygon->c = (Colorindex *)malloc(sizeof(float) * 4);
polygon->n=4;
polygon->polygon[X] = glcos;
polygon->polygon[Y] = glsin;
polygon->polygon[Z] = -0.1;
polygon->norm[X] = glcos;
polygon->norm[Y] = glsin;
polygon->norm[Z] = 0.0;
polygon->polygon[3+X] = glcos;
polygon->polygon[3+Y] = glsin;
polygon->polygon[3+Z] = s_cyl;
polygon->norm[3+X] = glcos;
polygon->norm[3+Y] = glsin;
polygon->norm[3+Z] = 0.0;
gl_sincos(a, &glsin, &glcos);
polygon->polygon[6+X] = glcos;
polygon->polygon[6+Y] = glsin;
polygon->polygon[6+Z] = s_cyl;
polygon->norm[6+X] = glcos;
polygon->norm[6+Y] = glsin;
polygon->norm[6+Z] = 0.0;
polygon->polygon[9+X] = glcos;
polygon->polygon[9+Y] = glsin;
polygon->polygon[9+Z] = -0.1;
polygon->norm[9+X] = glcos;
polygon->norm[9+Y] = glsin;
polygon->norm[9+Z] = 0.0;
}
}
edit_parts() {
float ct1[100][3], ct2[100][3];
float norm1[100][3], norm2[100][3];
Angle a;
float glsin, glcos;
int i;
Poly *polygon;
polygon = elbow;
for (i=0; i<num; i++) {
ct1[i][X] = mycirc[i][X];
ct1[i][Y] = mycirc[i][Y];
ct1[i][Z] = mycirc[i][Z];
norm1[i][X] = mycirc[i][X];
norm1[i][Y] = mycirc[i][Y];
norm1[i][Z] = mycirc[i][Z];
}
for (a=joint/bres; a<=joint; a += joint/bres) {
gl_sincos(a, &glsin, &glcos);
for (i=0; i<num; i++) {
ct2[i][X] = mycirc[i][X];
ct2[i][Y] = (mycirc[i][Y] - elbow_rad) * glcos + elbow_rad;
ct2[i][Z] = -(mycirc[i][Y]-elbow_rad) * glsin;
norm2[i][X] = mycirc[i][X];
norm2[i][Y] = mycirc[i][Y] * glcos;
norm2[i][Z] = -mycirc[i][Y] * glsin;
}
for (i=0; i<num-1; i++) {
polygon = polygon->next;
polygon->polygon[X] = ct1[i][X];
polygon->polygon[Y] = ct1[i][Y];
polygon->polygon[Z] = ct1[i][Z];
polygon->norm[X] = norm1[i][X];
polygon->norm[Y] = norm1[i][Y];
polygon->norm[Z] = norm1[i][Z];
polygon->polygon[3+X] = ct2[i][X];
polygon->polygon[3+Y] = ct2[i][Y];
polygon->polygon[3+Z] = ct2[i][Z];
polygon->norm[3+X] = norm2[i][X];
polygon->norm[3+Y] = norm2[i][Y];
polygon->norm[3+Z] = norm2[i][Z];
polygon->polygon[6+X] = ct2[i+1][X];
polygon->polygon[6+Y] = ct2[i+1][Y];
polygon->polygon[6+Z] = ct2[i+1][Z];
polygon->norm[6+X] = norm2[i+1][X];
polygon->norm[6+Y] = norm2[i+1][Y];
polygon->norm[6+Z] = norm2[i+1][Z];
polygon->polygon[9+X] = ct1[i+1][X];
polygon->polygon[9+Y] = ct1[i+1][Y];
polygon->polygon[9+Z] = ct1[i+1][Z];
polygon->norm[9+X] = norm1[i+1][X];
polygon->norm[9+Y] = norm1[i+1][Y];
polygon->norm[9+Z] = norm1[i+1][Z];
}
for (i=0; i<num; i++) {
ct1[i][X] = ct2[i][X];
ct1[i][Y] = ct2[i][Y];
ct1[i][Z] = ct2[i][Z];
norm1[i][X] = norm2[i][X];
norm1[i][Y] = norm2[i][Y];
norm1[i][Z] = norm2[i][Z];
}
}
polygon = double_cylinder;
gl_sincos(0, &glsin, &glcos);
for (a = 3600/cres; a<=3600; a += 3600/cres) {
polygon = polygon->next;
polygon->polygon[X] = glcos;
polygon->polygon[Y] = glsin;
polygon->polygon[Z] = -0.1;
polygon->norm[X] = glcos;
polygon->norm[Y] = glsin;
polygon->norm[Z] = 0.0;
polygon->polygon[3+X] = glcos;
polygon->polygon[3+Y] = glsin;
polygon->polygon[3+Z] = d_cyl;
polygon->norm[3+X] = glcos;
polygon->norm[3+Y] = glsin;
polygon->norm[3+Z] = 0.0;
gl_sincos(a, &glsin, &glcos);
polygon->polygon[6+X] = glcos;
polygon->polygon[6+Y] = glsin;
polygon->polygon[6+Z] = d_cyl;
polygon->norm[6+X] = glcos;
polygon->norm[6+Y] = glsin;
polygon->norm[6+Z] = 0.0;
polygon->polygon[9+X] = glcos;
polygon->polygon[9+Y] = glsin;
polygon->polygon[9+Z] = -0.1;
polygon->norm[9+X] = glcos;
polygon->norm[9+Y] = glsin;
polygon->norm[9+Z] = 0.0;
}
polygon = single_cylinder;
gl_sincos(0, &glsin, &glcos);
for (a = 3600/cres; a<=3600; a += 3600/cres) {
polygon = polygon->next;
polygon->polygon[X] = glcos;
polygon->polygon[Y] = glsin;
polygon->polygon[Z] = -0.1;
polygon->norm[X] = glcos;
polygon->norm[Y] = glsin;
polygon->norm[Z] = 0.0;
polygon->polygon[3+X] = glcos;
polygon->polygon[3+Y] = glsin;
polygon->polygon[3+Z] = s_cyl;
polygon->norm[3+X] = glcos;
polygon->norm[3+Y] = glsin;
polygon->norm[3+Z] = 0.0;
gl_sincos(a, &glsin, &glcos);
polygon->polygon[6+X] = glcos;
polygon->polygon[6+Y] = glsin;
polygon->polygon[6+Z] = s_cyl;
polygon->norm[6+X] = glcos;
polygon->norm[6+Y] = glsin;
polygon->norm[6+Z] = 0.0;
polygon->polygon[9+X] = glcos;
polygon->polygon[9+Y] = glsin;
polygon->polygon[9+Z] = -0.1;
polygon->norm[9+X] = glcos;
polygon->norm[9+Y] = glsin;
polygon->norm[9+Z] = 0.0;
}
}